<?php

include PATH_LIB . '/Upload.class.php';

class UserController extends CommonController {
	
	private $sqlite;

	public function __construct() {
		parent::__construct();
		$this->sqlite = $this->loadSqlite('bamboo');
	}
	
	public function profileAction(){
		$this->jsonSuccess('');
	}
	
	public function recordsAction() {
		$group = (int) $this->requestGet('group', 0);
		$conditions = $group ? " AND `group`={$group} " : '';

		$page = (int) $this->requestGet('page', 1);
		$pageSize = (int) $this->requestGet('pageSize', 100);
		$offset = ($page - 1) * $pageSize;

		$orderField = (int) $this->requestGet('orderField', 0);
		$orderDirection = (int) $this->requestGet('orderDirection', 1);
		$fields = array('group', 'name', 'ip', 'flag');
		$field = isset($fields[$orderField]) ? $fields[$orderField] : $field[0];
		$direction = $orderDirection > 0 ? 'ASC' : 'DESC';
		$order = " ORDER BY `{$field}` {$direction} ";

		$totalQuery = "SELECT COUNT(`id`) as `total` FROM `user`";

		$totalRow = $this->sqlite->getOne($totalQuery);
		$data = array('total' => $totalRow['total']);
		
		$query = "SELECT u.*, g.`name` as `groupName` FROM `user` u LEFT JOIN `group` g ON u.`group`=g.`id` WHERE 1 {$conditions} {$order} LIMIT {$offset}, {$pageSize}";
		$data['records'] = $this->sqlite->getAll($query);

		$this->jsonSuccess('', $data);
	}

	public function findAction() {
		$id = (int) $this->requestGet('id');
		if (!$id)
			$this->jsonError('参数错误');
		$findQuery = "SELECT * FROM `user` WHERE `id`={$id}";
		$row = $this->sqlite->getOne($findQuery);
		$this->jsonSuccess('', $row);
	}

	public function setAction(){
		$id = (int) $this->requestPost('id');

		$data = array(
			'group' => (int) $this->requestPost('group'),
			'name' => (string) $this->requestPost('name'),
			'ip' => (string) $this->requestPost('ip'),
			'avatar' => (string) $this->requestPost('avatar'),
			'flag' => (int) $this->requestPost('flag')
		);

		if ($this->sqlite->set('user', $data, $id)) {
			$this->jsonSuccess('保存成功');
		} else {
			$this->jsonError('保存失败 ' . $db->errorMessage);
		}
	}
	
	public function forbidAction(){}

	public function passAction(){}

	public function clearAction(){
		$query = "delete from `user`;delete from `sqlite_sequence` where name='user';";
		if ($this->sqlite->exec($query)) {
			$this->jsonSuccess('清理成功');
		} else {
			$this->jsonError('清理失败 ' . $db->errorMessage);
		}
	}

	public function changeAvatarAction() {
		$uid = (int) $_SESSION['uid'];

		$upload = new Upload(PATH_ROOT . '/upload/');

		$query = "SELECT `avatar` FROM `user` WHERE `id`={$uid}";

		$info = $this->sqlite->getOne($query);

		$path = PATH_ROOT . '/upload/' . $info['avatar'];
		$backup = $path . '.backup';
		if(file_exists($path)){
			@rename($path, $backup);
		}

		if($upload->single('avatar')){
			$data = array('avatar' => $upload->name);
			if($this->sqlite->set('user', $data, $uid)) {
				if(file_exists($backup)){
					@unlink($backup);
				}
				$this->jsonpSuccess('上传成功');
			} else {
				$this->jsonpSuccess('上传成功，更新信息失败。图片名称：' . $upload->name . $this->sqlite->errorMessage);
				if(file_exists($backup)){
					@rename($backup, $path);
				}
				@unlink($upload->path . $upload->name);
			}
		} else {
			if(file_exists($backup)){
				@rename($backup, $path);
			}
			$this->jsonpError('上传失败 ' . $upload->error);
		}
	}
}
?>
